package DAOImp;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import DAO.AbsHbnDao;
import DAO.CategoryDAO;
import POJO.Category;

@Repository
public class CategoryDaoImp extends AbsHbnDao<Category> implements CategoryDAO{

	//tim cac catagory cung level
	private static final String FIND_CATEGORY_BY_LEVEL = "FROM Category C WHERE C.priority = :level";
	private static final String FIND_CATEGORY_BY_PARENT_ID = "FROM Category C WHERE C.parentId = :parentId";
	private static final String FIND_MAX_LEVEL = "Select max(priority) from Category";
	
	@SuppressWarnings("unchecked")
	public List<Category> getCategories(int level) {
		Query query = getSession().createQuery(FIND_CATEGORY_BY_LEVEL).setParameter("level", level);
		return query.list();
	}

	public int getMaxLevel() {
		return (Integer) getSession().createQuery(FIND_MAX_LEVEL)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Category> getCategoriesByParentId(int parentId) {
		Query query = getSession().createQuery(FIND_CATEGORY_BY_PARENT_ID).setParameter("parentId", parentId);
		return query.list();
	}
}
